/* $Id:util.h jjs $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#ifndef UTIL_H
#define UTIL_H

#include <string>
#include <sigc++/signal.h>

#define nmin(a,b) (((a)<(b)?(a):(b))
#define nmax(a,b) (((a)>(b)?(a):(b))

/// Convert c-style string to int
int toint(char* s);

/// Convert string to int
int toint(std::string s);

/// Convert long to string
std::string tostr(long l);

/// Convert integer to string
std::string tostr(int i);

/// Convert c-style string to long
long tolong(char* s);

/// Convert string to long
long tolong(std::string s);

/// Copy the string n times in the result
std::string ncpy(int n, std::string s);

/// Replace '&', '<' and '>' tokens for xml export
std::string xml_encode(std::string s);

/// return the part of `parm` till the text `txt` or the whole `parm`, cut that part from `parm`
std::string split(std::string *parm, std::string txt);

class pos {
  public:
    pos(std::string p);
    pos(int ix=0, int iy=0) { x=ix; y=iy; };
    int x:16;
    int y:16;
};

struct pos_cmp {
    bool operator()( const pos a, const pos b) const {
        if (a.y<b.y) return true;
        if (a.y>b.y) return false;
        return a.x<b.x;
    };
};

/// Convert files including ~ tokens to their complete path
std::string filename(
    std::string fname  ///< the filename possibly including ~
);

/// Return if the given string is a file
bool isfile(
    std::string fname ///< check if this is a regular file
);

/// Read the given directory with the handler
void directory(std::string dir, sigc::signal<void, std::string> handler);

#endif // UTIL_H
